﻿using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Web.Mvc;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Concrete.Services;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Interfaces.Services;
using Tateeda.ClinicalStudy.Base.Web.Common;

#region - Legal -

///<legal>
/// Copyright © 2011, Tateeda Media Network
/// All rights reserved. http://tateeda.com
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions
/// are met:
///
/// - Redistributions of source code must retain the above copyright
/// notice, this list of conditions and the following disclaimer.
///
/// - Neither the name of the Tateeda Media Network, nor the names of its
/// contributors may be used to endorse or promote products
/// derived from this software without specific prior written
/// permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
/// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
/// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
/// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
/// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
/// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
/// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
/// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
/// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
///</legal>

#endregion - Legal -

namespace ClinicalStudy.Base.Web {

    public class MvcApplication : System.Web.HttpApplication {
        protected void Application_Start() {
            RegisterServices();
            RegisterDependencyResolver();

            AreaRegistration.RegisterAllAreas();            //Support for any future Area in the app.
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        private void RegisterDependencyResolver() {
            var kernel = new StandardKernel();

            //! ONLY ONE LOGGER Can be used at the time. !!!

            #region - Register Loggers -

            //User to log to file
            //kernel.Bind<ILogger>().To<FileLogger>();
            //Uncomment to log to DB
            //kernel.Bind<ILogger>().To<SqlLogger>();
            //Uncomment to log to Event Log
            //kernel.Bind<ILogger>().To<EventViewerLogger>();

            #endregion - Register Loggers -

            #region - Services binding --

            //AdminService must be registered firs with Service Locator and present in web config.
            kernel.Bind<IFormBuilder>().To<FormBuilderService>();
            kernel.Bind<IAdminService>().To<AdminService>();
            kernel.Bind<IApplicationService>().To<ApplicationService>();
            kernel.Bind<IAppointmentService>().To<AppointmentService>();
            kernel.Bind<IFormService>().To<FormService>();
            kernel.Bind<IReportingService>().To<ReportingService>();
            kernel.Bind<ISchedulingService>().To<SchedulingService>();
            kernel.Bind<ISiteService>().To<SiteService>();
            kernel.Bind<IStudyService>().To<StudyService>();
            kernel.Bind<ISubjectService>().To<SubjectService>();
            kernel.Bind<IUserService>().To<UserService>();
            kernel.Bind<IMedicationService>().To<MedicationService>();
            kernel.Bind<IMatrixService>().To<MatrixService>();
            kernel.Bind<IUtilityService>().To<UtilityService>();

            #endregion - Services binding --

            DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            /* //SAMPLE:
            routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
                               new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                               new {
                                       controller = "^H.*", action = "Index|About",
                                       httpMethod = new HttpMethodConstraint("GET", "POST"),
                                       customConstraint = new UserAgentConstraint("IE") // public class UserAgentConstraint : IRouteConstraint
                                   },
                               new[] { "URLsAndRoutes.Controllers" });
            */
        }

        private static void RegisterServices() {
            ServiceLocator.RegisterAllServices();
        }
    }
}